
// pages/reservation/index.js
Page({
  data: {   
    schedules: [
      {id: 1, time: '08:00', route: 'A校区 → B校区', seats: 10},
      {id: 2, time: '10:00', route: 'B校区 → A校区', seats: 5},
      {id: 3, time: '12:00', route: 'A校区 → C校区', seats: 15},
      {id: 4, time: '14:00', route: 'C校区 → A校区', seats: 8},
      {id: 5, time: '16:00', route: 'A校区 → B校区', seats: 12},
      {id: 6, time: '18:00', route: 'B校区 → A校区', seats: 0}
    ],
    currentSchedule: null,
    hasReservation: false,
    reservationInfo: {}
  },

  onLoad() {
    // 加载用户预约数据
    this.checkUserReservation();
  },

  checkUserReservation() {
    // 实际项目中这里应该调用API检查用户预约
    // 模拟数据
    const mockReservation = null; // 设为null测试无预约状态
    if(mockReservation) {
      this.setData({
        hasReservation: true,
        reservationInfo: mockReservation
      });
    }
  },

  selectSchedule(e) {
    this.setData({
      currentSchedule: e.currentTarget.dataset.id
    });
  },

  makeReservation() {
    const selected = this.data.schedules.find(
      item => item.id === this.data.currentSchedule
    );
    if (!selected) return;

    if (selected.seats <= 0) {
      wx.showToast({
        title: '该班次已无座位',
        icon: 'none'
      });
      return;
    }

    wx.showModal({
      title: '确认预约',
      content: `确定预约 ${selected.time} ${selected.route} 班车吗？`,
      success: (res) => {
        if (res.confirm) {
          // 实际项目这里调用预约API
          this.setData({
            hasReservation: true,
            reservationInfo: {
              id: selected.id,
              time: selected.time,
              route: selected.route,
              status: '已预约'
            }
          });
          wx.showToast({
            title: '预约成功',
            icon: 'success'
          });
        }
      }
    });
  },

  cancelReservation() {
    wx.showModal({
      title: '确认取消',
      content: '确定取消当前预约吗？',
      success: (res) => {
        if (res.confirm) {
          // 实际项目这里调用取消API
          this.setData({
            hasReservation: false,
            reservationInfo: {},
            currentSchedule: null
          });
          wx.showToast({
            title: '已取消预约',
            icon: 'success'
          });
        }
      }
    });
  }
});